import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import { useCounterStore } from '@/stores/counter';


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/Home',
      name: 'Home',
      component: () => import('../views/Home.vue'),
      children:[
        {
          path: '/about',
          name: 'about',
          // route level code-splitting
          // this generates a separate chunk (About.[hash].js) for this route
          // which is lazy-loaded when the route is visited.
          component: () => import('../views/AboutView.vue'),
        },
        {
          path: '/HomeView',
          name: 'HomeView',
          component: () => import('../views/HomeView.vue'),
        },
        {
          path: '/AddGoods',
          name: 'AddGoods',
          component: () => import('../views/Goods/AddGoods.vue'),
        },
        
        {
          path: '/ShowGoods',
          name: 'ShowGoods',
          component: () => import('../views/Goods/ShowGoods.vue'),
        },
        {
          path: '/UpdGoods/:id',
          name: 'UpdGoods',
          component: () => import('../views/Goods/UpdGoods.vue'),
        },
        {
          path: '/AddUser',
          name: 'AddUser',
          component: () => import('../views/Users/AddUser.vue'),
        },
        {
          path: '/ShowUser',
          name: 'ShowUser',
          component: () => import('../views/Users/ShowUser.vue'),
        },
        {
          path: '/UpdUserInfo/:id',
          name: 'UpdUserInfo',
          component: () => import('../views/Users/UpdUserInfo.vue'),
        },
        {
          path: '/AddPermission',
          name: 'AddPermission',
          component: () => import('../views/Users/AddPermission.vue'),
        },
        {
          path: '/ShowPermission',
          name: 'ShowPermission',
          component: () => import('../views/Users/ShowPermission.vue'),
        },
        {
          path: '/UpdPermission/:id',
          name: 'UpdPermission',
          component: () => import('../views/Users/UpdPermission.vue'),
        },
        {
          path: '/ShowOrders',
          name: 'ShowOrders',
          component: () => import('../views/Orders/ShowOrders.vue'),
        },
        {
          path: '/OrderCount',
          name: 'OrderCount',
          component: () => import('../views/Orders/OrderCount.vue'),
        },
      ]
    },
    //测试
    {
      path: '/Demo',
      name: 'Demo',
      component: () => import('../views/Demo.vue'),
    },
    //登录
    {
      path: '/',
      name: 'Login',
      component: () => import('../views/Users/Login.vue'),
    },
  ],
})

//通过导航守卫进行数据判断(to: 即将要进入的目标,from: 当前导航正要离开的路由,next：下一步操作)
router.beforeEach((to,from,next)=>{
  //创建store实例
  const store=useCounterStore();
  //如果未登录请跳转页面 不是登录页面时自动跳转到页面进行登录工作
  if(to.name!=="Login" && !store.isLogin)
  {
    //跳转到登录页面
    next("/")
  }
  else next();
})


export default router
